﻿using System;
using System.Data;
using System.Collections.Generic;
using DataLib.Common;
using DataLib.Model;

namespace DataLib.BLL
{
    /// <summary>
    /// InStorage
    /// </summary>
    public partial class InStorage
    {
         #region  ExtensionMethod

        /// <summary>
        /// 根据编号获取对象
        /// </summary>
        /// <param name="inStorageNo"></param>
        /// <returns></returns>
        public DataLib.Model.InStorage GetModelByNo(string inStorageNo)
        {
            return dal.GetModelByNo(inStorageNo);
        }

        /// <summary>
        /// 获取入库单维护数据集
        /// </summary>
        /// <returns></returns>
        public DataTable GetInMangeList(string city)
        {
            return dal.GetInMangeList(city);
        }

        /// <summary>
        /// 获取待提交的入库单的明细重
        /// </summary>
        /// <param name="instorid"></param>
        /// <returns></returns>
        public decimal GetWaitInWeight(int instorid)
        {
            return dal.GetWaitInWeight(instorid);
        }

        /// <summary>
        /// 联合查询，返回入库查询的所有明细项
        /// </summary>
        /// <param name="cumer">客户编码或者客户名称 只值客户编码和名称</param>
        /// <param name="strwhre">表名带条件查询，字符串类型注意加''  Instorage ins；Barcode bar；unProcessed un </param>
        /// <param name="intype">--入库方式, 2分类入库  1拆解入库 3生产入库, -1 临时库入库 0 是不选择入库方式</param>
        /// <returns></returns>
        public DataTable getDtByUnionWhere(string cumer, string strwhre, string intype)
        {
            return dal.getDtByUnionWhere(cumer, strwhre, intype);
        }



        /// <summary>
        /// 获取待入库列表，从出库单过来
        /// </summary>
        /// <param name="belongcity">归属地</param>
        /// <param name="grphiccity">处理地</param>
        /// <returns></returns>
        public DataTable getWaitInStorageList(string belongcity, string graphiccity)
        {
            return dal.getWaitInStorageList(belongcity, graphiccity);
        }

        
        /// <summary>
        /// 入库提交
        /// </summary>
        /// <param name="instorageid">入库ID</param>
        /// <param name="instype">--1拆解入库 2分类入库  3生产入库 5是移库入库 0 称重入库</param>
        /// <returns></returns>
        public int InstorageSubmit(int instorageid, string instype, string month, string remark,string man)
        {
            return dal.InstorageSubmit(instorageid, instype, month,remark,man);
        }
        /// <summary>
        /// 入库提交,存储过程
        /// </summary>
        /// <param name="instorageid">入库ID</param>
        /// <param name="instype">--1拆解入库 2分类入库  3生产入库 5是移库入库 6是直销入库 0 称重入库</param>
        /// <returns></returns>
        public int InstorageSubmit(int instorageid, string instype, string month)
        {
            return dal.InstorageSubmit(instorageid, instype,month);
        }

  
        /// <summary>
        /// 入库查询
        /// </summary>
        /// <param name="LoadNum">Load号</param>
        /// <param name="cum">客户</param>
        /// <param name="datetime1">入库日期起</param>
        /// <param name="datetime2">入库日期止</param>
        /// <param name="intye">入库方式</param>
        /// <param name="un_goodscode">处理前物品编码</param>
        /// <param name="bar_goodscode">处理后物品编码</param>
        /// <param name="un_ldh">处理前联单号</param>
        /// <param name="wrlno">入库单号</param>
        /// <param name="storageno">库位</param>
        /// <param name="bar_value">条码</param>
        /// <param name="boxid">箱号</param>
        /// <param name="belongcity">归属地</param>
        /// <param name="graphiccity">物理地</param>
        /// <returns></returns>
        public DataTable getInStorageItem(string LoadNum, string cum, string datetime1, string datetime2,
                            string intye, string un_goodscode, string bar_goodscode,
                            string un_ldh, string wrlno, string storageno, string bar_value, string boxid, string belongcity,
                            string graphiccity)
        {
            if (datetime2 != "" && datetime2 != null)
            {
                DateTime dtime = DateTime.Parse(datetime2);
                dtime = dtime.AddDays(1).AddSeconds(-1);
                datetime2 = dtime.ToString();
            }
            return dal.getInStorageItem(LoadNum, cum, datetime1, datetime2, intye, un_goodscode, bar_goodscode, un_ldh, wrlno, storageno, bar_value, boxid, belongcity, graphiccity);
        }

        /// <summary>
        /// 获取指定入库单的BarCode明细
        /// </summary>
        /// <param name="wrlid"></param>
        /// <param name="indate"></param>
        /// <returns></returns>
        public DataTable GetWrlItems(string wrlid, string indate)
        {
            return dal.GetWrlItems(wrlid, indate);
        }

        /// <summary>
        /// 根据入库单号查找
        /// </summary>
        ///<param name="datetime1">起</param>
        ///<param name="datetime2">止</param>
        ///<param name="wrlno">入库单号</param>
        /// <returns></returns>
        public DataTable GETItemsByInsNo(string datetime1, string datetime2, string wrlno)
        {
            if (datetime2 != null && datetime2.Trim() != "")
            {
                DateTime time = DateTime.Parse(datetime2);
                datetime2 = time.AddDays(1).AddSeconds(-1).ToString();
            }
            return dal.GETItemsByInsNo(datetime1, datetime2, wrlno);
        }

        /// <summary>
        /// 根据入库单号查找
        /// </summary>
        /// <returns></returns>
        public DataTable GETWRLItem(string loadnum,
                            string CJO,
                            string cum,
                            string peculiar,
                            string collman,
                            string dtime1,
                            string dtime2,
                            string wrlno,
                            string goodscode,
                            string boxid,
                            string barcon,
                            string isLocd,
                            string plateno)
        {
            if (dtime2 != null && dtime2.Trim() != "")
            {
                DateTime time = DateTime.Parse(dtime2);
                dtime2 = time.AddDays(1).AddSeconds(-1).ToString();
            }
            return dal.GETWRLItem(loadnum, CJO, cum, peculiar, collman, dtime1, dtime2, wrlno, goodscode, boxid, barcon, isLocd, plateno);
        }

               /// <summary>
        /// 客户查询
        /// </summary>
        /// <returns></returns>
        public DataTable GETWRLItem(string loadnum,
                             string cum,
                            string dtime1,      //收集日期起
                            string dtime2,      //收集日期止
                            string goodsName,   //物品名称
                            string barstatus    //已处理2 未处理1
                             )
        {
            if (dtime2 != null && dtime2.Trim() != "")
            {
                DateTime t = DateTime.Parse(dtime2.ToString());
                t =t.AddDays(1).AddSeconds(-1);
                dtime2 = t.ToString();
            }
            DataTable dt = dal.GETWRLItem(loadnum, cum, dtime1, dtime2, goodsName, barstatus, "");
             return dt;
        }

        /// <summary>
        /// 对BarCode表汇总
        /// </summary>
        /// <param name="loaddt"></param>
        /// <returns></returns>
        public DataTable GetTotal(DataTable loaddt)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("GoodsName");
            dt.Columns.Add("nwt");
            dt.Columns.Add("qty");
            string goodsname = "";
            decimal nwti = 0;
            int qtyi = 0;
            bool flag = false;
          
            for (int i = 0; i < loaddt.Rows.Count; i++)
            {
                flag = false;
                goodsname = loaddt.Rows[i]["bar_goodsname"].ToString();
                nwti = decimal.Parse(loaddt.Rows[i]["bar_nwt"].ToString());
                qtyi = int.Parse(loaddt.Rows[i]["bar_qty"].ToString());
                if (dt.Rows.Count > 0)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dt.Rows[j]["GoodsName"].ToString() == goodsname)
                        {
                            flag = true;
                            dt.Rows[j]["nwt"] = decimal.Parse(dt.Rows[j]["nwt"].ToString()) + nwti;
                            dt.Rows[j]["qty"] = int.Parse(dt.Rows[j]["qty"].ToString()) + qtyi;
                        }
                      
                    }
                }

                if (flag == false)
                {
                    DataRow dr = dt.NewRow();
                    dr["GoodsName"] = goodsname;
                    dr["nwt"] = nwti;
                    dr["qty"] = qtyi;
                    dt.Rows.Add(dr);
                }

 
            }
            return dt;
        }

        /// <summary>
        /// 获取入库单下面的重量
        /// </summary>
        /// <param name="insid"></param>
        /// <param name="weighttype"></param>
        /// <returns></returns>
        public decimal GetWeightByInsid(int insid)
        {
            return dal.GetWeightByInsId(insid, 1);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="weight"></param>
        /// <param name="insid"></param>
        /// <param name="weighttype">1是Barcode，2是辅料，3是损耗</param>
        /// <returns></returns>
        public bool GetConfirm(decimal weight, int insid, int weighttype)
        {
           bool flag = true;
            decimal barweight = dal.GetWeightByInsId(insid, 1);
             decimal flweight = dal.GetWeightByInsId(insid, 2);
            decimal shweight = dal.GetWeightByInsId(insid, 3);
            decimal outsweight = dal.GetWeightByInsId(insid, 4);
            switch (weighttype)
            {
                case 1:
                    barweight = barweight + weight;
                    break;
                case 2:
                    flweight = flweight + weight;
                    break;
                case 3:
                    shweight = shweight + weight;
                    break;
                default:
                    break;
            }
            if ((barweight + shweight) > (flweight + outsweight))
                flag = false;
            //desc = "出库单重量"　+　outsweight.ToString + " + 辅;
            return flag;
        }

         /// <summary>
        /// 得到入库单的总重量
        /// </summary>
        /// <param name="insid">入库单ID</param>
        public decimal GetInsTotalWeight(int insid)
        {
            return dal.GetInsTotalWeight(insid);
        }

        
        /// <summary>
        /// 修改入库单的条码的单一入库日期
        /// </summary>
        /// <param name="insid"></param>
        /// <param name="olddate"></param>
        /// <param name="newdate"></param>
        /// <returns></returns>
        public bool updateBarcodDate(string insid, string olddate, string newdate)
        {
            return dal.updateBarcodDate(insid, olddate, newdate);
        }

        
        /// <summary>
        /// 返回入库单ID
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int InsertPDNo(string bcity, string gcity, string operman)
        {
            return dal.InsertPDNo(bcity, gcity, operman);
        }

        
        /// <summary>
        /// 获取入库单维护数据集
        /// </summary>
        /// <returns></returns>
        public DataTable GetInsMangeList(string bcity,string gcity, int currpage, int pagesize, out int totalpage, string serach)
        {
            //return dal.GetInsMangeList(city,currpage,pagesize,out totalpage,serach);
            return dal.GetTab(bcity,gcity, currpage, pagesize, out totalpage, serach);
        }

        
        /// <summary>
        /// 2016年7月29日 21:30:49
        /// Redplum
        /// 待入库分页
        /// </summary>
        /// <param name="uerName"></param>
        /// <param name="currpage"></param>
        /// <param name="length"></param>
        /// <param name="totalRecord"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public DataTable GetWatiInsByStore(int currpage, int length, out int totalRecord, string search, string bcity, string gcity, string order_column, string order_dir, string uname)
        {
            return dal.GetWatiInsByStore(currpage, length, out totalRecord, search, bcity, gcity, order_column, order_dir);
        }

            /// <summary>
        /// 更新备注
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="remark"></param>
        /// <param name="insid"></param>
        /// <returns></returns>
        public string UpdateRemark(string uname, string remark, string insid)
        {
            return dal.UpdateRemark(uname, remark, insid);
        }
         #endregion  ExtensionMethod
    }
}

